package core;

import utils.UnSafeUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SqlInstance {


	SqlInstance(Connection conn,int state,String name) throws NoSuchFieldException {
		this.conn = conn;
		this.state = state;
		this.name = name;

		this.offset = UnSafeUtils.unsafe.objectFieldOffset(SqlInstance.class.getDeclaredField("state"));
	}
	long offset;

	Connection conn;

	String name;

	volatile int state;// 1 忙碌 0 空闲

	void execute(String  sql) throws SQLException {
		Statement statement = conn.createStatement();
		System.out.println(this.name+ "执行sql:" + sql);
		if(sql.startsWith("select")){
			ResultSet resultSet = statement.executeQuery(sql);
			while (resultSet.next()) {
				System.out.println(resultSet.getString("password"));
			}
		}else {
			int execute = statement.executeUpdate(sql);
		}
		statement.close();
	};
}
